package salab.report;

import java.io.IOException;
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class IncidentReportFormHandler
 */
public class IncidentReportFormHandler extends HttpServlet {
	private static final long serialVersionUID = 1L;

    /**
     * Default constructor. 
     */
    public IncidentReportFormHandler() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String name = request.getParameter( "name" );
		String sex = request.getParameter( "sex" );
		String address = request.getParameter( "address" );
		
		int month = Integer.parseInt( request.getParameter( "element_7_1" ) );
		int day = Integer.parseInt( request.getParameter( "element_7_2" ) );
		int year = Integer.parseInt( request.getParameter( "element_7_3" ) );
		Date bday = new Date(year, month, day);
		
		Date reportingDate = new Date( new java.util.Date().getTime() );
		
		String area = request.getParameter( "city" );
		
		String complaints = request.getParameter( "description" );
		
		String othersymptoms = request.getParameter( "othersymptoms" );
		String contacthistory = request.getParameter( "contacthistory" );
		String travelhostory = request.getParameter( "travelhostory" );
		
		Connection conn = null;
		String url = "jdbc:mysql://localhost:3306/";
		String dbName = "epidemiology";
		String driver = "com.mysql.jdbc.Driver";
		String userName = "root"; 
		String password = "root";
		try {
			Class.forName(driver).newInstance();
			conn = DriverManager.getConnection(url+dbName,userName,password);
			System.out.println("Connected to the database");
			
			String query = "INSERT INTO h1n1_incident VALUES (?,?,?,?,?,?,?,?,?,?)";
			PreparedStatement stmt = conn.prepareStatement( query );
			
			conn.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}

}
